const facModel = require('../model/facModel');

module.exports.find = async function (searchObj, paging) {
    // 页码 & 每页显示数
    let { current, pageSize } = paging;
    // // 获取数据总数
    let total = await facModel.countDocuments(searchObj);
    let data = await facModel.find(searchObj).populate({ path: 'films' }).populate({ path: 'cinemas' }).limit(pageSize).skip((current - 1) * pageSize);
    return {
        current, pageSize, total, rows: data
    }
}

module.exports.findFacAll = async function (filmId, cinemaId) {
    return await facModel
        .find({ films: filmId, cinemas: cinemaId })
        .populate({ path: 'films' })
        .populate({ path: 'cinemas' });
}

module.exports.findAll = async function () {
    return await facModel.find();
}

module.exports.insert = async function (data) {
    return await facModel.create(data);
}

module.exports.delete = async function (id) {
    return await facModel.remove({ _id: id });
}

module.exports.findById = async function (id) {
    return await facModel.findById(id).populate({ path: 'films' }).populate({ path: 'cinemas' });
}

module.exports.update = async function (id, data) {
    return await facModel.updateOne({ _id: id }, data);
}